package com.fanfanwang.ffw.app.tools;

import java.util.ArrayList;
import com.fanfanwang.ffw.app.common.LogUtil;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class SharedPreferencesUtil {

	/**
	 * 文件名
	 */
	private static final String sh_data_name = "tinghaoche_share_data";
	/**
	 * 是否登陆：true进入首页，false进入登陆页
	 */
	private static final String is_login = "is_login";
	/**
	 * 所属片区
	 */
	private static final String pianqu = "pianqu";
	/**
	 *停车场名称
	 */
	private static final String name = "name";
	/**
	 * 用户id
	 */
	private static final String user_id = "user_id";
	/**
	 * 用户name
	 */
	private static final String username = "username";
	/**
	 * 用户carnumber 车牌号
	 */
	private static final String carnumber = "carnumber";
	/**
	 * 用户truename 真实名字
	 */
	private static final String truename = "truename";
	/**
	 * 经营人
	 */
	private static final String transactor = "transactor";
	
	/**
	 * 收费类型 ：0为按次数收费，1为按小时收费
	 */
	private static final String chargetype = "chargetype";
	
	/**
	 * 每次（小时）收费
	 */
	private static final String chargeprice = "chargeprice";
	/**
	 * 提醒哪里的设置 是否需要接收闹钟提醒
	 */
	private static final String isReceiveAlarmClock = "isReceiveAlarmClock";
	/**
	 * 提醒时间(这里指的是最后15分钟，或者最后20分钟，或者最后10分钟)
	 */
	private static final String remindtime= "remindtime";
	/**
	 * 提醒的铃声
	 */
	private static final String remindbell= "remindbell";
	/**
	 * 提醒的铃声 pos
	 */
	private static final String remindbellpos= "remindbellpos";
	/**
	 * 提醒的时候是否开启震动
	 */
	private static final String remindvibration= "remindvibration";
	/**
	 * 提醒的时候是否开启震动
	 */
	private static final String nowAddress= "nowAddress";
	/**
	 * token
	 */
//	private static final String my_token = "token";
//	private static final String last_album_id = "last_album_id";
	
	private static SharedPreferences saveInfo;
	private static Editor saveEditor;
	private volatile static SharedPreferencesUtil uniqueInstance;

//	private static final String SINA_TOKEN = "sina_token";
//	private static final String SINA_EXIRES_IN = "sina_exires_in";
//	private static final String SINA_SECRET = "sina_secret";
//	private static final String QQ_TOKEN = "qq_token";
//	private static final String QQ_EXIRES_IN = "qq_exires_in";
//
//	private static final String IS_RECEIVE = "is_receive";
//	private static final String IS_SOUND = "is_sound";
//	private static final String IS_SHOCK = "is_shock";
//	
//	private static final String IS_FRIST_HOME = "is_frist_home";
//	private static final String IS_FRIST_MONEY = "is_frist_money";
//	private static final String IS_FRIST_TREASURE = "is_frist_treasure";
//	
	
	

	
	
	private SharedPreferencesUtil() {
	}

	public static SharedPreferencesUtil getInstance(Context context) {
		if (uniqueInstance == null) {
			synchronized (SharedPreferencesUtil.class) {
				if (uniqueInstance == null) {
					uniqueInstance = new SharedPreferencesUtil();
					saveInfo = context.getSharedPreferences(sh_data_name, Context.MODE_PRIVATE);
					saveEditor = saveInfo.edit();
				}
			}
		}
		return uniqueInstance;
	}

	public  int getRemindbellpos() {
		return saveInfo.getInt(remindbellpos, 0);
	}
	public  boolean putRemindbellpos(int pos) {
		saveEditor.putInt(remindbellpos, pos);
		return saveEditor.commit();
	}
	public  boolean getIsreceivealarmclock() {
		return saveInfo.getBoolean(isReceiveAlarmClock, false);
	}
	public  boolean putIsreceivealarmclock(boolean isReceiveAlarmClock1) {
		saveEditor.putBoolean(isReceiveAlarmClock, isReceiveAlarmClock1);
		return saveEditor.commit();
	}
	public  String getRemindtime() {
		return saveInfo.getString(remindtime, null);
	}
	public  boolean putRemindtime(String remindtime1) {
		saveEditor.putString(remindtime, remindtime1);
		return saveEditor.commit();
	}

	public  String getRemindbell() {
		return saveInfo.getString(remindbell, null);
	}
	public  boolean putRemindbell(String remindbell1) {
		saveEditor.putString(remindbell, remindbell1);
		return saveEditor.commit();
	}

	public  boolean getRemindvibration() {
		return saveInfo.getBoolean(remindvibration, false);
	}
	public  boolean putRemindvibration(boolean remindvibration1) {
		saveEditor.putBoolean(remindvibration, remindvibration1);
		return saveEditor.commit();
	}


	public  String getNowAddress() {
		return saveInfo.getString(nowAddress, null);
	}
	public  boolean putNowAddress(String nowAddress1) {
		saveEditor.putString(nowAddress, nowAddress1);
		return saveEditor.commit();
	}

	public String getPainqu() {
		return saveInfo.getString(pianqu, null);
	}
	
	public boolean putPianqu(String pianqu) {
		saveEditor.putString(pianqu, pianqu);
		return saveEditor.commit();
	}


	
	public  boolean putUsername(String username1) {
		saveEditor.putString(username, username1);
		return saveEditor.commit();
	}
	public  String getUsername() {
		return saveInfo.getString(username, null);
	}

	public  boolean putCarnumber(String carnumber1) {
		saveEditor.putString(carnumber, carnumber1);
		return saveEditor.commit();
	}
	public  String getCarnumber() {
		return saveInfo.getString(carnumber, null);
	}
	public  boolean putTruename(String truename1) {
		saveEditor.putString(truename, truename1);
		return saveEditor.commit();
	}
	public  String getTruename() {
		return saveInfo.getString(truename, null);
	}

	public String getName() {
		return saveInfo.getString(name, null);
	}
	
	public boolean putName(String name1) {
		saveEditor.putString(name, name1);
		return saveEditor.commit();
	}
	public String getUserId() {
		return saveInfo.getString(user_id, null);
	}

	public boolean putUserId(String id) {
		saveEditor.putString(user_id, id);
		return saveEditor.commit();
	}
	public String getTransactor() {
		return saveInfo.getString(transactor, null);
	}
	
	public boolean putTransactor(String transactor) {
		saveEditor.putString(transactor, transactor);
		return saveEditor.commit();
	}
	public String getChargetype() {
		return saveInfo.getString(chargetype, null);
	}
	
	public boolean putChargetype(String chargetype) {
		saveEditor.putString(chargetype, chargetype);
		return saveEditor.commit();
	}
	public String getChargeprice() {
		return saveInfo.getString(chargeprice, null);
	}
	
	public boolean putChargeprice(String chargetype) {
		saveEditor.putString(chargeprice, chargetype);
		return saveEditor.commit();
	}

//	public String getToken() {
//		return saveInfo.getString(my_token, null);
//	}
//
//	public boolean putToken(String token) {
//		saveEditor.putString(my_token, token);
//		return saveEditor.commit();
//	}
//
//	public String getLastAlbumId() {
//		return saveInfo.getString(last_album_id, null);
//	}
//
//	public boolean putLastAlbumId(String lastAlbumId) {
//		saveEditor.putString(last_album_id, lastAlbumId);
//		return saveEditor.commit();
//	}

	/**
	 * 储存列表信息
	 * @param sKey
	 * @return
	 */
	public static boolean saveArray(ArrayList<String> sKey) {
		saveEditor.putInt("Status_size",sKey.size()); /*sKey is an array*/ 
		LogUtil.d("saveArray", "  >> " + sKey.size());
	    for(int i=0;i<sKey.size();i++) {
	    	saveEditor.remove("Status_" + i);
	    	saveEditor.putString("Status_" + i, sKey.get(i));  
	    }

	    return saveEditor.commit();     
	}
	
	/**
	 * 取列表信息
	 * @param mContext
	 * @param sKey
	 */
	public static void loadArray(Context mContext,ArrayList<String> sKey) {  
	    sKey.clear();
	    int size = saveInfo.getInt("Status_size", 0);  
	    LogUtil.d("loadArray", "  >> " + size);
	    for(int i=0;i<size;i++) {
	        sKey.add(saveInfo.getString("Status_" + i, null));  
	    }
	}
	
	// sina-----授权
//	public boolean putSinaExiresIn(String flag) {
//		saveEditor.putString(SINA_EXIRES_IN, flag);
//		return saveEditor.commit();
//	}
//
//	public String getSinaExiresIn() {
//		return saveInfo.getString(SINA_EXIRES_IN, null);
//	}
//
//	public boolean putSinaSecret(String flag) {
//		saveEditor.putString(SINA_SECRET, flag);
//		return saveEditor.commit();
//	}
//
//	public String getSinaSecret() {
//		return saveInfo.getString(SINA_SECRET, null);
//	}
//
//	public boolean putSinaToken(String flag) {
//		saveEditor.putString(SINA_TOKEN, flag);
//		return saveEditor.commit();
//	}
//
//	public String getSinaToken() {
//		return saveInfo.getString(SINA_TOKEN, null);
//	}
//
//	public void cleanSina() {
//		saveEditor.remove(SINA_TOKEN);
//		saveEditor.remove(SINA_EXIRES_IN);
//		saveEditor.commit();
//	}
//
//	// qq------授权
//	public boolean putQQExiresIn(String flag) {
//		saveEditor.putString(QQ_EXIRES_IN, flag);
//		return saveEditor.commit();
//	}
//
//	public String getQQExiresIn() {
//		return saveInfo.getString(QQ_EXIRES_IN, null);
//	}
//
//	public boolean putQQToken(String flag) {
//		saveEditor.putString(QQ_TOKEN, flag);
//		return saveEditor.commit();
//	}
//
//	public String getQQToken() {
//		return saveInfo.getString(QQ_TOKEN, null);
//	}
//
//
//	public void cleanQQ() {
//		saveEditor.remove(QQ_TOKEN);
//		saveEditor.remove(QQ_EXIRES_IN);
//		saveEditor.commit();
//	}
//
	/**
	 * @param key
	 * @param value
	 */
//	public void saveSerializable(String key, Object value) {
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//		try {
//			ObjectOutputStream oos = new ObjectOutputStream(baos);
//			oos.writeObject(value);
//			oos.flush();
//			if (oos != null)
//				oos.close();
//			String strData = new String(Base64.encodeBase64(baos.toByteArray()));
//			saveEditor.putString(key.toLowerCase(), strData);
//			saveEditor.commit();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				if (baos != null)
//					baos.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}

//	/**
//	 * @param _key
//	 * @return
//	 */
//	public Object restoreSerializable(String _key) {
//		String key = _key.toLowerCase();
//		Object obj = null;
//		String objectInfo = saveInfo.getString(key, null);
//		if (objectInfo == null) {
//			return null;
//		}
//		ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(objectInfo.getBytes()));
//		try {
//			ObjectInputStream ois = new ObjectInputStream(bais);
//			obj = ois.readObject();
//			ois.close();
//		} catch (StreamCorruptedException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				if (bais != null)
//					bais.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return obj;
//	}
	public static void  putLoginDate(Context context,String loginDate){
		SharedPreferences sharedPreferences= context.getSharedPreferences("logindate", context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("logindate", loginDate);
		editor.commit();
	}
	public static String getLoginDate(Context context){
		SharedPreferences sharedPreferences= context.getSharedPreferences("logindate", context.MODE_PRIVATE);
		return sharedPreferences.getString("logindate", "");
	}
}
